# -*- coding: utf-8 -*-
# @时间      : 2025/4/9 下午3:13
# @作者      : 德云测-熊猫
#导入等待时间
from time import sleep
#导入启动包
from selenium import webdriver
#导入chrome服务(车)
from selenium.webdriver.chrome.service import Service
#导入一个定位元素工具 By (方向盘)
from selenium.webdriver.common.by import By
#导入两个包 第一个 等待包
from selenium.webdriver.support.wait import WebDriverWait
#定位状态的包 期望状态 状态满足了 我就定位 需要把expected_conditions 改名为 EC
from selenium.webdriver.support import expected_conditions as EC

#车里装进去一个司机
service = Service(executable_path='chromedriver.exe')
#司机发动Chrome这个车
driver = webdriver.Chrome(service=service)
#初始化 等待
wait = WebDriverWait(driver,10,0.5)  #等待的司机放到wait,最长等10秒,每0.5秒看一次


#开启网页
driver.get("https://deyunce:828123@mall.deyunce.com/pc/")
#百度全屏
driver.maximize_window()
#隐式等待 全局等待
driver.implicitly_wait(10)
"""
我需要做的事情
"""
#进行登录操作
#等待我看到登录按键
wait.until(EC.visibility_of_element_located((By.XPATH,"//a[@href=\"/pc/account/login\"]")))  #注意括号最后三个
ele1 = driver.find_element(By.XPATH,"//a[@href=\"/pc/account/login\"]")  #第二步不用动 上面一步看到了 定位过来
wait.until(EC.element_to_be_clickable(ele1)) #判断一下是不是能点
ele1.click()  #如果可以点再


#切换成账号密码登录
wait.until(EC.visibility_of_element_located((By.XPATH,"//div[contains(text(),'账号密码登录')]")))
ele2 = driver.find_element(By.XPATH,"//div[contains(text(),'账号密码登录')]")
wait.until(EC.element_to_be_clickable(ele2))
ele2.click()

# #输入手机号
wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@placeholder="请输入账号/手机号码"]')))
ele3 = driver.find_element(By.XPATH,'//input[@placeholder="请输入账号/手机号码"]')
wait.until(EC.visibility_of(ele3))  #我可以给ele3 输入文字
ele3.send_keys("13613613636")

#输入密码
wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@placeholder="请输入密码"]')))
ele4 = driver.find_element(By.XPATH,'//input[@placeholder="请输入密码"]')
wait.until(EC.visibility_of(ele4))
ele4.send_keys("A123456")


#如果使用find elements  a 是一个列表 点击第二个要写下标1
wait.until(EC.visibility_of_any_elements_located((By.XPATH,'//span[contains(text(),\'立即登录\')]'))) #这里要any
ele5 = driver.find_elements(By.XPATH,'//span[contains(text(),\'立即登录\')]')[1]
wait.until(EC.element_to_be_clickable(ele5))
ele5.click()


#商品搜索 输入熊猫
wait.until(EC.visibility_of_element_located((By.XPATH,'//input[@placeholder="请输入要搜索的商品名称"]')))
ele6 = driver.find_element(By.XPATH,'//input[@placeholder="请输入要搜索的商品名称"]')
wait.until(EC.visibility_of(ele6))
ele6.send_keys("熊猫")

#点击搜索按键
wait.until(EC.visibility_of_element_located((By.XPATH,"//div[contains(text(),\"搜索\")]")))
ele7 = driver.find_element(By.XPATH,"//div[contains(text(),\"搜索\")]")
wait.until(EC.element_to_be_clickable(ele7))
ele7.click()

#选择 一只公熊猫点击
wait.until(EC.visibility_of_element_located((By.XPATH,'//div[text()=\'一只公熊猫\']')))
ele8 = driver.find_element(By.XPATH,'//div[text()=\'一只公熊猫\']')
wait.until(EC.element_to_be_clickable(ele8))
ele8.click()

#点击立即购买
wait.until(EC.visibility_of_element_located((By.XPATH,'//div[text()="立即购买"]')))
ele9 = driver.find_element(By.XPATH,'//div[text()="立即购买"]')
wait.until(EC.element_to_be_clickable(ele9))
ele9.click()

#处理alert 加上 智能等待
wait.until(EC.alert_is_present())
driver.switch_to.alert.accept()

#点击提交订单
wait.until(EC.visibility_of_element_located((By.XPATH,'//div[contains(text(),"提交订单")]')))
ele10 = driver.find_element(By.XPATH,'//div[contains(text(),"提交订单")]')
wait.until(EC.element_to_be_clickable(ele10))
ele10.click()

#点击账户余额
wait.until(EC.visibility_of_element_located((By.XPATH,'//span[text()="账户余额"]')))
ele11 = driver.find_element(By.XPATH,'//span[text()="账户余额"]')
wait.until(EC.element_to_be_clickable(ele11))
ele11.click()

sleep(5)
#可以不写 关闭 浏览器
driver.quit()